<?php
class TeacherDao {
	public function get($id){
		return ClassManager::getSql()->onedata('teacher','*',"where id =".intval($id));
	}

	public function getDataPairList($strs_list){
		$strs = implode(',', $strs_list);
		return ClassManager::getSql()->dataPairList('teacher','id,name,serial_number,skype,qq'," where id in (".$strs.")");
	}

	public function getNoBookTeacherByDateTime($where=array(),$limit=0){
		$where = $this->searchMutilWhere($where);
		$sql = " select a.id,a.`name`,serial_number,skype,qq from eo_teacher a ";
		$sql .= " left join eo_teacher_class b on a.id=b.teacher_id"; 
		$sql .= " where 1=1 and  a.status='1' and b.status='1' ";
		$sql .= $where;
		return ClassManager::getSql()->dataMutillist('teacher and teacher_class',$sql,$limit);
	}
	
	public function searchMutilWhere($where){
		$where1 = '';
		
		if(isset($where['confirm_status'])&&$where['confirm_status']>0){
			$where1 .= " and b.confirm_status = '".$where['confirm_status']."'";
		}
		
		if(isset($where['book_time'])&&$where['book_time']>0){
			$where1 .= " and b.time = '".$where['book_time']."'";
		}
		if(isset($where['start_book_time'])&&$where['start_book_time']>0){
			$where1 .= " and b.time >= '".$where['start_book_time']."'";
		}
		
		if(isset($where['book_date'])&&$where['book_date']>0){
			$where1 .= " and b.date = '".$where['book_date']."'";
		}
		
		if(isset($where['name'])&&!empty($where['name'])){
			$where1 .= " and a.name like  '%".$where['name']."%'";
		}
		
		if(isset($where['serial_number'])&&!empty($where['serial_number'])){
			$where1 .= " and a.serial_number like  '%".$where['serial_number']."%'";
		}
		
		if(isset($where['status'])&&!empty($where['status'])){
			$where1 .= " and a.status = '".$where['status']."'";
		}
		
		if(isset($where['teacher_openclass_status'])&&$where['teacher_openclass_status']>-1){
			$where1 .= " and b.status = '".$where['teacher_openclass_status']."'";
		}
		
		if(isset($where['teacher_id_list'])&&!empty($where['teacher_id_list'])){
			$where1 .= " and a.id in (".$where['teacher_id_list'].")";
		}
		
		return $where1;
	}
	
	
	public function add($data){
		ClassManager::getSql()->insert('teacher', $data);
	}
	public function update($data,$id){
		ClassManager::getSql()->updates('teacher', $data,"where id=".intval($id));
	}
	
	public function searchList($where=array(),$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1('teacher','*',$where,$order,$limit);
	}
	
	
	public function searchOpenclassList($where=array(),$order='',$limit=0){
		$where = $this->searchMutilWhere($where);
		$sql = " select * from ";
		$sql .= " (select a.id,a.pxh,a.`name`,serial_number,skype,a.phone,qq,b.id as teacher_class_id,b.status,b.confirm_status,b.date as  open_date,b.time as open_time from eo_teacher a ";
		$sql .= " left join eo_teacher_class b on a.id=b.teacher_id"; 
		$sql .= " where 1=1 ";
		$sql .= $where;
		$sql .= " ) c order by c.pxh,c.open_date,c.open_time asc ";
		return ClassManager::getSql()->dataMutillist('teacher and open_teacher_lists ',$sql,$limit);
	}
	
	public function searchOpenclassNumber($where){
		$where = $this->searchMutilWhere($where);
		$sql = " select count(1) cnt from ";
		$sql .= " (select a.id,a.`name`,serial_number,skype,qq,b.status,b.confirm_status from eo_teacher a ";
		$sql .= " left join eo_teacher_class b on a.id=b.teacher_id"; 
		$sql .= " where 1=1 ";
		$sql .= $where;
		$sql .= " ) a ";
		$tmp = ClassManager::getSql()->dataMutillist('teacher and open_teacher_lists cnt ',$sql);
		return $tmp[0]['cnt'];
	}
	
	public function searchNumber($where){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1('teacher',"count(*)  ",$where);
		return $tmp[0];
	}
	public function searchWhere($where){
		$where1 = array();
		$likeWhere = array('serial_number','name');
		foreach ($where as  $k=>$v){
			if (!empty($v)){
				if (in_array($k,$likeWhere) ){
					$where1[$k] = array('%'.$v.'%' , 'like');
				}
				else{
					$where1[$k] =  $v;
				}
			}
		}
		return $where1;
	}
	public function deleteBatchByid($id){
		ClassManager::getSql()->delete('teacher',"where id in(".$id.")");
	}
	public function getBySerialNumber($serialNumber){
		return ClassManager::getSql()->onedata('teacher','*',"where serial_number='".$serialNumber."'");
	}
	public function batchUpdateManagement($employeesId,$id){
		$data['employees_id'] = $employeesId ; 
		ClassManager::getSql()->updates('teacher', $data,"where id in(".$id.")");
	}
}

?>